<?php

namespace App;

use Illuminate\Routing\ResourceRegistrar;

class BaseResourceRegistrar extends ResourceRegistrar
{
    protected function getResourceAction($resource, $controller, $method, $options)
    {
        $name = $this->getResourceRouteName($resource, $method, $options);

        // 获取资源型路由设置的别名及权限
        $alias_permission = $this->getResourceRouteAliasPermission($resource, $method, $options);

        $action = [
            'as' => $name,
            'alias' => $alias_permission['alias'],
            'permission' => $alias_permission['permission'],
            'index' => $alias_permission['index'],
            'uses' => $controller . '@' . $method
        ];

        if (isset($options['middleware'])) {
            $action['middleware'] = $options['middleware'];
        }

        if (isset($options['excluded_middleware'])) {
            $action['excluded_middleware'] = $options['excluded_middleware'];
        }

        if (isset($options['wheres'])) {
            $action['where'] = $options['wheres'];
        }

        if (isset($options['missing'])) {
            $action['missing'] = $options['missing'];
        }
        return $action;
        // return parent::getResourceAction($resource, $controller, $method, $options); // TODO: Change the autogenerated stub
    }

    protected function getResourceRouteAliasPermission($resource, $method, $options)
    {
        $name = $resource;

        // If the aliasPermissions array has been provided to us we will check for an entry in the
        // array first. We will also check for the specific method within this array
        // so the aliasPermissions may be specified on a more "granular" level using methods.
        // 资源型路由别名权限设置方式
        /*
        Route::apiResource('nature', 'NatureController', ['aliasPermissions' => [
            'index' => ['课程性质.浏览', true,'index'],
            'show' => ['课程性质.详情', true],
            'store' => ['课程性质.添加', true],
            'destroy' => ['课程性质.删除', true],
            'update' => ['课程性质.修改', true],
        ]]);
        Route::apiResource('article', 'ArticleController')->only(['index', 'show'])->aliasPermissions([
            'index' => '文章列表', // 等同于 'index' => ['文章列表', false],
            'show' => '查看文章内容', // 等同于 'show' => ['查看文章内容', false],
        ]);
        */
        if (isset($options['aliasPermissions'])) {
            if (is_string($options['aliasPermissions'])) {
                $name = $options['aliasPermissions'];
            } elseif (isset($options['aliasPermissions'][$method])) {
                if (is_array($options['aliasPermissions'][$method])) {
                    $alias = !empty($options['aliasPermissions'][$method][0]) ? $options['aliasPermissions'][$method][0] : null;
                    $permission = !empty($options['aliasPermissions'][$method][1]) ? $options['aliasPermissions'][$method][1] : false;
                    $index = !empty($options['aliasPermissions'][$method][2]) && $options['aliasPermissions'][$method][2] == 'index' ? true : false;
                    $alias_permission = [
                        'alias' => $alias,
                        'permission' => $permission,
                        'index' => $index,
                    ];
                } else {
                    $alias_permission = [
                        'alias' => $options['aliasPermissions'][$method],
                        'permission' => false,
                        'index' => false,
                    ];
                }
                return $alias_permission;
            }
        }

        // If a global prefix has been assigned to all names for this resource, we will
        // grab that so we can prepend it onto the name when we create this name for
        // the resource action. Otherwise we'll just use an empty string for here.
        $prefix = isset($options['as']) ? $options['as'] . '.' : '';
        $alias_permission = [
            'alias' => trim(sprintf('%s%s.%s', $prefix, $name, $method), '.'),
            'permission' => false,
            'index' => false,
        ];

        return $alias_permission;
    }
}
